package com.augrain.demo.controller.session_attributes;

import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;

/**
 * 测试SessionAttribute
 *
 * @author biaoy
 * @date 2021/8/14
 */
@RestController
@RequestMapping("/book")
@SessionAttributes(value = {"book", "description"}, types = {Double.class})
public class RedirectController {
    @GetMapping("/index")
    public String index(Model model) {
        model.addAttribute("book", "Java");
        model.addAttribute("description", "The most develop language");
        model.addAttribute("price", new Double("1000.00"));
        //book、description和price会被保存Session中，因为注解@SessionAttribute中有这几个参数
        return "success";
    }

    @GetMapping("/get")
    public String get(@SessionAttribute("book") String book, ModelMap model,
                      SessionStatus sessionStatus) {
        System.out.println(book + ";" + model.getAttribute("description") + ";" + model.getAttribute("price"));

        // 会清除数据
        // sessionStatus.setComplete();
        return "success";
    }

    @GetMapping("/complete")
    public String complete(ModelMap modelMap) {
        //已经被清除，无法获取book的值
        System.out.println(modelMap.get("book"));
        modelMap.addAttribute("book", "妹纸");
        return "sessionAttribute";
    }
}
